package com.zero.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.entity.FamilyMember;
import com.zero.entity.InvestType;
import com.zero.mapper.FamilyMemberMapper;
import com.zero.mapper.InvestTypeMapper;
import com.zero.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class InvestTypeService {
    
    @Autowired
    private InvestTypeMapper investTypeMapper;

    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    public Result add(InvestType investType, HttpServletRequest request){
        if (investType == null)
            return Result.fail("添加投资分类失败");

        //获取用户信息
        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到用户信息");
        familyMember = familyMemberMapper.selectById(familyMember.getId());

        investType.setFamilyId(familyMember.getFamilyId());
        int row = investTypeMapper.add(investType);
        if (row == 1)
            return Result.ok("添加投资分类成功");
        return Result.fail("添加投资分类失败");
    }

    @Transactional
    public Result delete(String ids){
        if (ids == null)
            return Result.fail("无法获取要删除的项");
        String[] idSet = ids.split(",");
        int row = 0;
        for (String id : idSet) {
            row += investTypeMapper.delete(Integer.parseInt(id));
        }
        if (row > 0)
            return Result.ok("成功删除"+ row +"条记录");
        return Result.fail("删除失败");
    }

    public Result update(InvestType investType){
        if (investType == null)
            return Result.fail("获取修改信息失败");
        int row = investTypeMapper.update(investType);
        if (row == 1)
            return Result.ok("修改投资分类成功");
        return Result.fail("修改投资分类失败");
    }

    public Result select(InvestType investType, HttpServletRequest request){
        if (investType == null )
            return Result.fail("获取不到查询的详细信息");

        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() == null)
            return Result.fail("获取不到用户信息");
        familyMember = familyMemberMapper.selectById(familyMember.getId());
        investType.setFamilyId(familyMember.getFamilyId());

        //分页查询信息
        if (investType.getPage() != null ){
            if (investType.getLimit() != null)
                PageHelper.startPage(investType.getPage(), investType.getLimit());
            else
                PageHelper.startPage(investType.getPage(), 10);
        }else{
            PageHelper.startPage(1, 10);
        }

        List<InvestType> investTypes = investTypeMapper.select(investType);
        PageInfo pageInfo = new PageInfo(investTypes);

        return Result.ok(investTypes, pageInfo.getTotal());

    }

    public Result select(HttpServletRequest request){
        FamilyMember familyMember = (FamilyMember) request.getAttribute("familyMember");
        if (familyMember == null || familyMember.getId() ==null)
            return Result.fail("获取不到用户信息");
        familyMember = familyMemberMapper.selectById(familyMember.getId());

        InvestType investType = new InvestType();
        investType.setFamilyId(familyMember.getFamilyId());

        List<InvestType> investTypes = investTypeMapper.select(investType);
        return Result.ok(investTypes);
    }
    
}
